from helpers import Visualization
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app


class MainPage(webapp.RequestHandler):
    """ Load graphs to the Iframe """
    def get(self):
        
        ret_graph_text = Visualization.createGraphForTwoUsers(users.get_current_user(),users.User(self.request.get('user2')))
        
        self.response.out.write("""<html><head><script type="text/javascript" src="http://www.google.com/jsapi"></script> <script type="text/javascript">
            google.load("visualization", "1", {packages:["linechart"]});
            google.setOnLoadCallback(drawChartGraph);
            function drawChartGraph() {
                var dataGraph = new google.visualization.DataTable();
                dataGraph.addColumn('string', 'Date');
                """)
        self.response.out.write(ret_graph_text)
        self.response.out.write("""
                var chartGraph = new google.visualization.LineChart(document.getElementById('graph_div'));""")
        self.response.out.write("chartGraph.draw(dataGraph, {'width': 400, 'height': 300, 'legend': 'bottom', 'title': '%s VS. %s'});" % (users.get_current_user().nickname().capitalize() , users.User(self.request.get('user2')).nickname().capitalize()))
        self.response.out.write("""  }
          </script>
            </head>
            <body>
            <div id='graph_div'></div>
            </body>
            </html>""")


application = webapp.WSGIApplication(
                                     [('/compareUsers.html', MainPage)],
                                     debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()